package com.leoman.spinningmachine.tab1;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.leoman.helper.util.LogUtil;
import com.leoman.helper.util.RefreshUtil;
import com.leoman.helper.util.ToastUtil;
import com.leoman.spinningmachine.R;
import com.leoman.spinningmachine.base.BaseActivity;
import com.leoman.spinningmachine.bean.LeavespBean;
import com.leoman.spinningmachine.bean.MyMessageBean;
import com.leoman.spinningmachine.constant.Constant;
import com.leoman.spinningmachine.http.APIResponse;
import com.leoman.spinningmachine.http.WebServiceApi;
import com.leoman.spinningmachine.util.BGARefreshUtil;
import com.leoman.spinningmachine.util.DialogUtils;
import com.leoman.spinningmachine.util.SharedPreferencesUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

/**
 * 请假审批（管理）
 */
public class LeaveSpActivity extends BaseActivity implements LeaveSpAdapter.onSpClick {

    @BindView(R.id.bgaRefreshLayout)
    BGARefreshLayout bgaRefreshLayout;
    @BindView(R.id.rv)
    RecyclerView recyclerView;
//    private List<LeaveGroup> list = new ArrayList<>();
    private LeaveSpAdapter adapter;
    private List<LeavespBean> spList = new ArrayList<>();
    private String token;
    private String userId;
    private String userNickName;
    private int page;
    private boolean isLastPage;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.ac_qytz;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        token = SharedPreferencesUtils.getInstance().getString("token");

        adapter = new LeaveSpAdapter(this,R.layout.item_leave_sp,null);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        if(getIntent()!=null){
            MyMessageBean bean = (MyMessageBean) getIntent().getSerializableExtra("data");
            userId = bean.getUserId();
            userNickName = bean.getNickname();
            initTitle(""+userNickName);
            LogUtil.json("userId:"+userId);
            LogUtil.json("userNickName:"+userNickName);
        }

        page = 1;
        bgaRefreshLayout.setDelegate(this);
        RefreshUtil.getInstance().initRefresh(this,bgaRefreshLayout);
        bgaRefreshLayout.beginRefreshing();
    }

    @Override
    public void OnSuccessData(APIResponse apiResponse, Integer tag) {
        if(3 == tag){
            DialogUtils.getInstance().cancel();
            ToastUtil.showToast(this, "" + apiResponse.message, Toast.LENGTH_SHORT);
            WebServiceApi.getInstance().queryUserLeaveList(token,userId,"",""+Constant.pageSize,this,1);
        } else {
            BGARefreshUtil.endLoading(tag,bgaRefreshLayout);
            isLastPage = BGARefreshUtil.getIsLastPage(apiResponse);
            if(page == 1){
                spList.clear();
            }
            spList.addAll(apiResponse.data.list);
            adapter.setData(spList);
            adapter.setSpClick(this);
        }
    }
    @Override
    public void sp(int id, int position, int type) {
        DialogUtils.getInstance().show(this);
        WebServiceApi.getInstance().updateLeave(token,""+id,""+type,this,3);
    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        super.onBGARefreshLayoutBeginRefreshing(refreshLayout);
        page = 1;
        WebServiceApi.getInstance().queryUserLeaveList(token,userId,""+page,""+Constant.pageSize,this,1);
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        if(isLastPage){
            return false;
        }
        page++;
        DialogUtils.getInstance().show(this);
        WebServiceApi.getInstance().queryUserLeaveList(token,userId,""+page,""+Constant.pageSize,this,2);
        return true;
    }
}
